{include file="public/head"/}
<style type="text/css">
    .layui-table-cell{height: auto!important;}
    .layui-layer-tips{width: auto!important;}
</style>
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">退款原因列表</div>
        <div class="layui-card-body layui-form">
            <div class="table-head-btn" style="margin-bottom: 10px;">
                
            </div>
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}
<script type="text/html" id="toolbar">
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="direct">删除</button>
    <a class="layui-btn layui-btn-sm" lay-event="add">添加退款原因</a>
</script>


<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
    <p>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </p>
</script>
<script>
    layui.config({
        base: '__LAYADMIN__/',
        version: 20220407
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['table', 'form'],function (){
        var $ = layui.$, admin = layui.admin, table = layui.table, form = layui.form;
        //全部数据
        tableIn = table.render({
            elem: '#list',
            url: '{:url(\'OrderReason/index\')}',
            method: 'post',
            toolbar: '#toolbar',
            defaultToolbar:[],
            autoSort: false,
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', title: '编号', width: 80, fixed: true, sort:true},
                {field: 'title', title: '退款原因', align:"center", width: 280},
                {field: 'sort', title: '排序',align:"center", width: 120,sort: true,edit:'text'},
                {field: 'status', align: 'center', title: '状态', width: 120, toolbar: '#status',sort:true},
                {field: 'create_time', title: '创建时间', width: 180,sort:true},
                {field: 'update_time', title: '编辑时间', width: 180,sort:true},
                {width: 180, align: 'center', toolbar: '#action'}
            ]],
            page: true
        });

        //监听排序事件
        table.on('sort(list)', function(obj){
            table.reload('list', {
                initSort: obj
                ,where: {sort_by: obj.field,sort_order: obj.type}
            });
        });

        //改变会员状态
        form.on('switch(status)', function (obj) {
            var param={};
            param[obj.elem.name] = obj.elem.checked===true?1:0;
            param['id'] = this.value;
            cf(param);
        });

        table.on('edit(list)', function(obj){
            var param={};
            param[obj.field] = obj.value;
            param['id'] = obj.data.id;
            cf(param);
        });

        function cf(param){
            admin.req({
                url: '{:url(\'OrderReason/cf\')}',
                data: param,
                type:'post',
                success: function (res) {
                    if(res.code==0){
                        layer.msg(res.msg,{time:1000,icon:1});
                    }else{
                        tableIn.reload();
                    }
                }
            });
        }
        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            var id = data.id;
            if (obj.event === 'edit') {
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'OrderReason/edit\')}?id=' + id,
                    area: ['50%', '80%'],
                    maxmin: true
                });
                // layer.full(index);
            }else if(obj.event === 'del'){
                layer.confirm('确定删除此退款原因吗？', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'OrderReason/del\')}'
                        , type: 'post'
                        , data: {id:id}
                        , success: function (res) {
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }
        });

        //监听工具条
        table.on('toolbar(list)', function (obj) {
            if (obj.event === 'add') {
                var index = layer.open({
                    type: 2,
                    content: '{:url(\'OrderReason/add\')}',
                    area: ['50%', '80%'],
                    maxmin: true
                });
                // layer.full(index);
            }else if(obj.event=='direct'){
                var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return layer.msg('未选中行',{icon:2});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['id'];
                });
                layer.confirm('确定删除所选退款原因吗？', function(){
                    layer.closeAll('dialog');
                    admin.req({
                        url: '{:url(\'OrderReason/del\')}'
                        , type: 'post'
                        , data: {id:ids}
                        , success: function (res) {

                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
            }
        });

        var active = {
            reload: function () {
                var model = $('#model').val();
                var type = $('#type').val();
                //执行重载
                table.reload('list', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {model:model,type:type}
                });
            }
        };
        $('.table-head-btn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>